package com.vf.cloud.paas.app.master.api.product.controller;

import java.util.List;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.jfinal.kit.StrKit;
import com.vf.cloud.paas.app.master.common.domain.ProductFun;
import com.vf.cloud.paas.common.datasource.annotation.Tx;
import com.vf.cloud.paas.common.log.annotation.RLog;
import com.vf.cloud.paas.common.security.annotation.TokenRequired;
import com.vf.cloud.paas.common.util.R;
import com.vf.cloud.paas.common.util.UuidUtil;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1/product/fun")
public class ProductFunController {

	@RLog(value = "获取组件功能列表", type = "user")
	@TokenRequired
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public R<List<ProductFun>> list(@RequestBody ProductFun productFun) {
		String sqlExceptSelect = " from " + ProductFun.TABLE_NAME + "  a WHERE a.product_id='"
				+ productFun.getProductId() + "' ";
		sqlExceptSelect += "  ORDER BY a.name  , a.modify_time desc ";
		if (!StrKit.isBlank(productFun.getName())) {
			sqlExceptSelect += " and name like '%" + productFun.getName() + "%' ";
		}
		return R.ok(ProductFun.dao.find("select *  " + sqlExceptSelect));
	}

	@RLog(value = "保存组件功能", type = "user")
	@TokenRequired
	@Tx
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public R<ProductFun> save(@RequestBody ProductFun productFun) {
		try {

			if (StrKit.notBlank(productFun.getId())) {
				if (productFun.update()) {
					return R.ok(productFun);
				} else {
					return R.failed("更新失败！");
				}
			} else {
				productFun.setId(UuidUtil.getUUID());
				if (productFun.save()) {
					return R.ok(productFun);
				} else {
					return R.failed("保存失败！");
				}
			}
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}

	@RLog(value = "删除组件功能", type = "user")
	@TokenRequired
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public R<String> delete(@RequestBody ProductFun productFun) {
		if (productFun.delete()) {
			return R.ok();
		}
		return R.failed();
	}

}
